#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'

void solve() {
	int n; cin >> n;
	vector<int> a(n + 1);
	for (int i = 1; i <= n; i++) cin >> a[i];
	int x = 0, y = 0;
	for (int i = 1; i <= n; i++) {
		if (a[i] == a[1]) x++;
		else break;
	}
	for (int i = n; i >= 1; i--) {
		if (a[i] == a[n]) y++;
		else break;
	}
	if (a[1] == a[n]) {
		if (x == n && y == n) cout << 0 << endl;
		else cout << n - x - y << endl;
	}
	else {
		cout << n - max(x, y) << endl;
	}
}

int main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int t; cin >> t;
	while (t--) {
		solve();
	}
	return 0;
}